/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.dsw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.biblioteca8a.BL.CategoriaBLLocal;
import mx.itschapala.dsw2.biblioteca8a.modelo.Categoria;



/**
 *
 * @author Raven
 */
@ManagedBean
@SessionScoped
public class CategoriaBean {
    @EJB
    private CategoriaBLLocal categoriaBL;
     /**
     * Creates a new instance of CategoriaBean
     */
    public CategoriaBean() {
    }
    
    //-----------------------Variables----------------
    private List<Categoria>lista;
    private Categoria category;
    private String accion;
    private int n;

    
    //------------------------Metodos GET y SET----------------
    public List<Categoria> getLista() {
        lista=categoriaBL.getAll();
        return lista;
    }

    public Categoria getCategory() {
        return category;
    }

    public void setCategory(Categoria category) {
        this.category = category;
    }
    
    
    //-----------------------Acciones---------------
        public String procesarAccion(){
        System.out.println("Accion '':"+accion);
        if(accion.equalsIgnoreCase("Nuevo")){
        categoriaBL.register(category);
        }
        else
        if(accion.equalsIgnoreCase("Editar")){
            categoriaBL.modify(category);
        }
        else if(accion.equalsIgnoreCase("Eliminar")){
            categoriaBL.delete(category);
        }
        accion="";
    return "CategoriaLista";
    }
    
    
   public String limpiarAccion(){
   category=new Categoria();
   return "CategoriaCrearEditar";
    }
   
   //-----------------Listener------------------------------

public void prepararNuevo(ActionEvent evt){
category=new Categoria();
accion="Nuevo";
}
public void prepararEditar(ActionEvent evt)   
{
  
System.out.println(evt.getComponent().getClientId());
 n=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    System.out.println("el numero es:"+n);
    category=lista.get(n);
    accion="Editar";
}     


public void prepararEliminar(ActionEvent evt)   
{
System.out.println(evt.getComponent().getClientId());
 n=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
    System.out.println("el numero es:"+n);
    category=lista.get(n);
    accion="Eliminar";
} 
    
    
    
    
    
    
    

   
}
